package cn.dnui_lf_705.dnui_16110100705.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.dnui_lf_705.dnui_16110100705.R;
import cn.dnui_lf_705.dnui_16110100705.bean.CommentBean;
import cn.dnui_lf_705.dnui_16110100705.controller.BuyActivity;
import cn.dnui_lf_705.dnui_16110100705.controller.AddCommentActivity;


public class BuyAdapter extends RecyclerView.Adapter<BuyAdapter.ViewHolder> implements View.OnClickListener {
    private List<CommentBean> mDataList;
     private Context mcontext;
     private LayoutInflater mLayoutInflater;
    public BuyAdapter(BuyActivity context, List Comment) {
        this.mDataList = Comment;
        this.mcontext = context;
        mLayoutInflater = LayoutInflater.from(mcontext);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BuyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v =mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.food_item,viewGroup,false);
        v.setOnClickListener(this);
        return new BuyAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder1, final int i) {
        final CommentBean entity = mDataList.get(i);
        if(entity == null)
            return;
        final BuyAdapter.ViewHolder viewHolder = viewHolder1;
        viewHolder.food_name.setText(entity.getFoodname());
        viewHolder.price.setText(entity.getPrice()*entity.getNum()+"元");
        viewHolder.food_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                        //跳转到菜谱详细页
                        String str1 = String.valueOf(mDataList.get(i).getItem_id());
                        Intent intent = new Intent(mcontext,AddCommentActivity.class);
                        intent.putExtra("item_id",str1);
                        mcontext.startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView food_name,food_pinglun,price;
        ImageView image;
        LinearLayout food_container;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            food_name = itemView.findViewById(R.id.food_name);
            food_pinglun = itemView.findViewById(R.id.res_pinglun);
            price = itemView.findViewById(R.id.price);
            image = itemView.findViewById(R.id.food_image);
            food_container = itemView.findViewById(R.id.ll_food_list);
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

}
